﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WhaleAccount.Models;

/// <summary>
/// 日收支汇总
/// </summary>
public class DayRecordSummary : INotifyPropertyChanged
{
    private DateTime date;
    private double income;
    private double expense;
    private bool isSelectale;
    private bool isEditable = false;
    private readonly ObservableCollection<Record> records = new();
    private readonly ObservableCollection<Record> selectedRecords = new();

    public DateTime Date
    {
        get => date;
        set
        {
            if (date != value)
            {
                date = value;
                OnPropertyChanged();
            }
        }
    }
    public double Income
    {
        get => income; 
        set 
        {
            var round = Math.Round(value, 2);
            if (income != round) {
                income = round;
                OnPropertyChanged();
            }
        }
    }
    public double Expense
    {
        get => expense;
        set
        {
            var round = Math.Round(value, 2);
            if (expense != round)
            {
                expense = round;
                OnPropertyChanged();
            }
        }
    }

    /// <summary>
    /// 收支记录是否可被选中
    /// </summary>
    public bool IsSelectale
    {
        get => isSelectale;
        set
        {
            if (isSelectale != value)
            {
                isSelectale = value;
                OnPropertyChanged();
            }
        }
    }

    /// <summary>
    /// 收支记录是否可编辑
    /// </summary>
    public bool IsEditable
    {
        get => isEditable;
        set
        {
            if (isEditable != value)
            {
                isEditable = value;
                OnPropertyChanged();
            }
        }
    }

    public ObservableCollection<Record> Records => records;

    public ObservableCollection<Record> SelectedRecords => selectedRecords;


    public DayRecordSummary()
    {

    }

    public DayRecordSummary(DateTime date) : this(date, 0, 0)
    {

    }

    public DayRecordSummary(DateTime date, double income, double expense)
    {
        Date = date;
        Income = income;
        Expense = expense;
    }

    public void Calculate()
    {
        double income = 0;
        double expense = 0;
        foreach (var record in records)
        {
            if (record.Category == 1)
            {
                income += record.Value;
            }
            else
            {
                expense += record.Value;
            }
        }

        Income = income;
        Expense = expense;
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        // Raise the PropertyChanged event, passing the name of the property whose value has changed.
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
